package com.ftg.learn.controller;

import com.ftg.learn.enity.SysUser;
import com.ftg.learn.service.IUserService;
import com.ftg.learn.util.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户控制器类
 *
 * @author wangty
 * @date 2022/10/20
 */
@RestController
@Api(tags = "系统管理")
@RequestMapping("/login")
public class LoginController {

    @Resource
    IUserService iUserService;

    /**
     * 系统登录功能
     * @param u_account 账号
     * @param u_password 密码
     * @return
     *
     */
    @PostMapping("/checkUserForLogin")
    public ResultJson checkUser(
            @RequestParam(value = "u_account") @ApiParam(value = "帐号",required = true,example = "admin") String u_account,
            @RequestParam(value = "u_password") @ApiParam(value = "密码",required = true,example = "admin") String u_password

    ){
        return iUserService.loginForUser(u_account,u_password);
    }

    /**
     * 判断当前给定的u_id具有哪些角色，以及角色具有全部权限
     * @param u_id
     * @return
     */
    @GetMapping("/showPrivilege")
    public ResultJson showPrivilege(
            @RequestParam(value = "u_id") @ApiParam(value = "用户编号",required = true,example = "1") Integer u_id

    ){
        return iUserService.showPrivilegeWithUid(u_id);
    }

    /**
     * 添加用户，注册账号，创建用户
     * 按照实例注册
     * @return
     */
    @ApiOperation(value = "创建用户")
    @PostMapping("/creatAll")
    public ResultJson creatAll(
             SysUser sysUser,
            @RequestParam(value = "emp_name") @ApiParam(value = "员工姓名",required = true,example = "张三") String emp_name,
            @RequestParam(value = "emp_sex") @ApiParam(value = "员工性别",required = true,example = "男/女") String emp_sex,
            @RequestParam(value = "emp_code") @ApiParam(value = "员工学号/职工号",required = true,example = "t1001") String emp_code,
            @RequestParam(value = "emp_type") @ApiParam(value = "员工类型",example = "1001") Integer emp_type,
            @RequestParam(value = "emp_job",defaultValue = "普通员工") @ApiParam(value = "员工岗位",example = "普通员工") String emp_job,
            @RequestParam(value = "emp_birthday") @ApiParam(value = "员工生日",example = "xxxx-xx-xx") String emp_birthday,
            @RequestParam(value = "clazz_id") @ApiParam(value = "班级编号",required = true,example = "1") Integer clazz_id ){

        return ResultJson.OK(iUserService.creatAll(sysUser,emp_name,emp_sex,
                emp_code,emp_type,emp_job,emp_birthday,clazz_id));
    }

    /**
     * 重置密码
     * @param sysUser User实例
     * @return
     */
    @ApiOperation(value = "忘记密码")
    @PutMapping("/changePassword")
    public int changePassword(SysUser sysUser){
        return iUserService.changePassword(sysUser);
    }


}
